package com.restaurant.controller;

import com.restaurant.client.ProductRecordClient;
import com.restaurant.result.Result;
import com.restaurant.vo.ShowVo;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.sdk.v3.BcosSDK;
import org.fisco.bcos.sdk.v3.client.Client;
import org.fisco.bcos.sdk.v3.client.RespCallback;
import org.fisco.bcos.sdk.v3.client.protocol.response.BcosBlock;
import org.fisco.bcos.sdk.v3.client.protocol.response.BlockNumber;
import org.fisco.bcos.sdk.v3.model.Response;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;

@Slf4j
@RestController
@RequestMapping("/api")
public class ShowController {

    static ShowVo showVo = new ShowVo();


    public static final String configFile = ProductRecordClient.class.getClassLoader().getResource("config-example.toml").getPath();

    static RespCallback<BcosBlock> bcosBlockRespCallback1 = new RespCallback<BcosBlock>() {
        @Override
        public void onResponse(BcosBlock bcosBlock) {
            showVo.setDataHash1(bcosBlock.getBlock().getHash());
            showVo.setGasUse1(bcosBlock.getBlock().getGasUsed());
            showVo.setVersion1(bcosBlock.getBlock().getVersion());
            showVo.setId1(bcosBlock.getId());
            showVo.setTransactionsRoot1(bcosBlock.getResult().getTransactionsRoot());

        }

        @Override
        public void onError(Response response) {
            // 在这里处理错误
            System.err.println("错误: " + response);
        }
    };
    static RespCallback<BcosBlock> bcosBlockRespCallback2 = new RespCallback<BcosBlock>() {
        @Override
        public void onResponse(BcosBlock bcosBlock) {
            showVo.setDataHash2(bcosBlock.getBlock().getHash());
            showVo.setGasUse2(bcosBlock.getBlock().getGasUsed());
            showVo.setVersion2(bcosBlock.getBlock().getVersion());
            showVo.setId2(bcosBlock.getId());
            showVo.setTransactionsRoot2(bcosBlock.getResult().getTransactionsRoot());

        }

        @Override
        public void onError(Response response) {
            // 在这里处理错误
            System.err.println("错误: " + response);
        }
    };
    static RespCallback<BcosBlock> bcosBlockRespCallback3 = new RespCallback<BcosBlock>() {
        @Override
        public void onResponse(BcosBlock bcosBlock) {
            showVo.setDataHash3(bcosBlock.getBlock().getHash());
            showVo.setGasUse3(bcosBlock.getBlock().getGasUsed());
            showVo.setVersion3(bcosBlock.getBlock().getVersion());
            showVo.setId3(bcosBlock.getId());
            showVo.setTransactionsRoot3(bcosBlock.getResult().getTransactionsRoot());
        }

        @Override
        public void onError(Response response) {
            // 在这里处理错误
            System.err.println("错误: " + response);
        }
    };

    RespCallback<BlockNumber> blockNumberRespCallback = new RespCallback<BlockNumber>() {

        @Override
        public void onResponse(BlockNumber blockNumber) {

            //设置块高
            showVo.setNumber(blockNumber.getResult());
        }

        @Override
        public void onError(Response response) {
            // 在这里处理错误
            System.err.println("错误: " + response);
        }
    };

    @PostMapping("/show")
    public Result<ShowVo> changeStatus() {

        BcosSDK sdk = BcosSDK.build(configFile);
        Client client = sdk.getClient("group0");

//        client.getBlockNumberAsync(blockNumberRespCallback);
        BlockNumber blockNumber = client.getBlockNumber();
        String result = blockNumber.getResult();
        System.out.println(result);
        showVo.setNumber(result);
        result(client);
        return Result.success(showVo);
    }

    public static void result(Client client){
        client.getBlockByNumberAsync(BigInteger.valueOf(Long.parseLong(showVo.getNumber())), false, false, bcosBlockRespCallback1);
        client.getBlockByNumberAsync(BigInteger.valueOf(Long.parseLong(showVo.getNumber()) - 1), false, false, bcosBlockRespCallback2);
        client.getBlockByNumberAsync(BigInteger.valueOf(Long.parseLong(showVo.getNumber()) - 2), false, false, bcosBlockRespCallback3);
    }
}
